#!/bin/bash -e

print_url=0
timeout=10

from_sta='Redfern'
dest_sta='Blacktown'

old_now_date=$(date +%d%%2F%m%%2F%y)
old_now_time=$(date +%I%%3A%M%p)

now_date=$(date +%Y%m%d)
now_hour=$(date +%I)
now_min=$(date +%M)
now_ampm=$(date +%P)

function usage()
{
echo
cat <<EOF
[*] usage: `basename $0`  options

OPTIONS:
	-u	print url
	-h	show this message
	-a	set from station
	-b	set dest. station
	-t	set search time (08:30AM)
	-d	set search date (21/5/10)
EOF
echo
exit
}

function get_cookie()
{
	wget -q -O /dev/null --keep-session-cookies --save-cookies="cookie.txt" "${1}"
}

while getopts "uha:b:t:d:" flag
do
	if [ "$flag" == 'u' ]
	then
		print_url=1
	fi

	if [ "$flag" == 'h' ]
	then
		usage
	fi

	if [ "$flag" == 'a' ]
	then
		A=$(echo -n "${OPTARG:0:1}" | tr "[:lower:]" "[:upper:]") 
		from_sta="$(echo -n "${A}${OPTARG:1}" | sed 's/ /+/g')"
	fi

	if [ "$flag" == 'b' ]
	then
		A=$(echo -n "${OPTARG:0:1}" | tr "[:lower:]" "[:upper:]") 
		dest_sta="$(echo -n "${A}${OPTARG:1}" | sed 's/ /+/g')"
	fi

	if [ "$flag" == 't' ]
	then
		now_time=$(echo -n "$OPTARG" | tr '[:lower:]' '[:upper:]')
		now_time=$(echo "$now_time" | sed 's/://g;s/\-//g;s/\///g;s/\\//g')

		now_hour=$(echo -n "${now_time:0:2}")
		now_min=$(echo -n "${now_time:2:2}")
		now_ampm=$(echo -n "${now_time:4:2}")
	fi
	
	if [ "$flag" == 'd' ]
	then
		now_date="$OPTARG"
		now_date=$(echo "$now_date" | sed 's/://g;s/\-//g;s/\///g;s/\\//g')
	fi
done

old_url="http://www.131500.com.au/fullEnquiry.asp?&Vehicle=Bus,Train,Ferry&WalkSpeed=NORMAL&&IsAfter=A&MaxChanges=-1&FromLocType=s&ToLocType=s&x=64&y=11&FromLoc=$from_sta+Station~~;$from_sta+Station;$from_sta+Station~~LOCATION&ToLoc=$dest_sta+Station~~;$dest_sta+Station;$dest_sta+Station~~LOCATION&Time=$now_time&Date=$now_date"

ori_url='http://www.131500.com.au/plan-your-trip/trip-planner?session=invalidate&itd_cmd=invalid&itd_includedMeans=checkbox&itd_inclMOT_5=1&itd_inclMOT_7=1&itd_inclMOT_1=1&itd_inclMOT_9=1&itd_anyObjFilter_origin=2&itd_name_origin=redfern&itd_anyObjFilter_destination=2&itd_name_destination=blacktown&itd_itdDate=20100824&itd_itdTripDateTimeDepArr=dep&itd_itdTimeHour=2&itd_itdTimeMinute=0&itd_itdTimeAMPM=pm&x=56&y=11'

url="http://www.131500.com.au/plan-your-trip/trip-planner?session=invalidate&itd_cmd=invalid&itd_includedMeans=checkbox&itd_inclMOT_5=1&itd_inclMOT_7=1&itd_inclMOT_1=1&itd_inclMOT_9=1&itd_anyObjFilter_origin=2&itd_name_origin=${from_sta}&itd_anyObjFilter_destination=2&itd_name_destination=${dest_sta}&itd_itdDate=${now_date}&itd_itdTripDateTimeDepArr=dep&itd_itdTimeHour=${now_hour}&itd_itdTimeMinute=${now_min}&itd_itdTimeAMPM=${now_ampm}&x=56&y=11"

if [ $print_url == '1' ]
then
	echo $url
fi

echo
echo "From: $from_sta Station"
echo "Dest: $dest_sta Station"
echo "Time: $(echo $now_hour:$now_min $now_ampm | sed 's/\%3A/:/g')"
echo "Date: $(echo ${now_date:0:4}:${now_date:4:2}:${now_date:6:2} | sed 's/\%2F/\//g')"
echo

referer='http://www.131500.com.au/'
agent='Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8 (.NET CLR 3.5.30729)'

#get_cookie $url
#wget $url -q -O - --referer "$referer" --load-cookies=cookie.txt --user-agent="$agent" | html2text | grep 'Option \([0-9]\+\)\|Arr:\|Dep:'
lynx -noredir -dump $url | grep 'Option \([0-9]\+\)\|Arr:\|Dep:'

echo
